﻿using System;
using System.Windows.Forms;

namespace ControlTest
{
	public partial class Form1 : Form
	{
		//private ListViewGroupingSet[] allGroupings;

		public Form1()
		{
			InitializeComponent();

			treeView1.HandleCreated += treeView1_HandleCreated;
			customListView1.HandleCreated += customListView1_HandleCreated;

			/*var gp = new GenericParsing.GenericParserAdapter() { FirstRowHasHeader = true, FirstRowSetsExpectedColumnCount = true };
			gp.SetDataSource(@"C:\Users\dahall\Personal\Church\Membership.csv");
			dataListView1.DataMember = "Table1";
			dataListView1.DataSource = gp.GetDataSet();
			dataListView1.Columns["Birth"].FormatProvider = new S2DFP();
			dataListView1.Columns["Birth"].Format = "d";*/

			/*var items = new ComboItem[] { new ComboItem(120, "120", false), new ComboItem(20, "20"), new ComboItem(230, "230", false), new ComboItem(342, "342"), new ComboItem(344, "344", false) };

			comboBox1.DisplayMember = "Text"; comboBox1.ValueMember = "ID";
			comboBox1.Items.AddRange(items);

			superComboBox1.Items.AddRange(items);

			allGroupings = new ListViewGroupingSet[] {
				new ListViewGroupingSet() { 
					Name = "Bool", 
					Groupings = new ListViewGroup[] { new ListViewGroup("True"), new ListViewGroup("False") }, 
					GroupingRules = new Predicate<ListViewItem>[] { delegate(ListViewItem i) { return (bool)i.SubItems[1].Tag; }, delegate(ListViewItem i) { return !(bool)i.SubItems[1].Tag; } }
				}
			};

			// Sort items
			for (int i = 0; i < dataListView1.Columns.Count; i++)
				sortByToolStripMenuItem.DropDownItems.Insert(i, new ToolStripMenuItem(dataListView1.Columns[i].Text, null, sortHandler) { Tag = i });

			// Group items
			for (int i = 0; i < allGroupings.Length; i++)
				groupByToolStripMenuItem.DropDownItems.Insert(i, new ToolStripMenuItem(allGroupings[i].Name, null, groupHandler) { Tag = i });
			*/
		}

		void customListView1_HandleCreated(object sender, EventArgs e)
		{
			customListView1.SetExplorerTheme();
		}

		void treeView1_HandleCreated(object sender, EventArgs e)
		{
			treeView1.SetExplorerTheme();
		}

		/*private void groupHandler(object sender, EventArgs e)
		{
			ToolStripMenuItem i = sender as ToolStripMenuItem;
			if (i.Tag is int)
			{
				int grpg = (int)i.Tag;
				dataListView1.ApplyGroupingSet(allGroupings[grpg]);
			}
			else
				dataListView1.ShowGroups = false;
		}*/

		private void sortHandler(object sender, EventArgs e)
		{
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
		}

		private class S2DFP : IFormatProvider, ICustomFormatter
		{
			public object GetFormat(Type formatType) => (formatType is ICustomFormatter) ? this : null;

			public string Format(string format, object arg, IFormatProvider formatProvider)
			{
				DateTime dt;
				if (arg is string && DateTime.TryParse((string)arg, out dt))
					return dt.ToString(format, formatProvider);
				return HandleOtherFormats(format, arg, formatProvider);
			}

			private string HandleOtherFormats(string format, object arg, IFormatProvider formatProvider)
			{
				if (arg is IFormattable)
					return ((IFormattable)arg).ToString(format, formatProvider);
				else if (arg != null)
					return arg.ToString();
				else
					return String.Empty;
			}
		}

		private class ComboItem // : IEnableable
		{
			public ComboItem(int id, string txt, bool enabled = true)
			{
				ID = id; Text = txt; Enabled = enabled;
			}

			public int ID { get; }
			public string Text { get; }
			public bool Enabled { get; }

			public override string ToString() => this.Text;
		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{

		}

		private void Form1_Load(object sender, EventArgs e)
		{
			// TODO: This line of code loads data into the 'northwindDataSet.Products' table. You can move, or remove it, as needed.
			//this.productsTableAdapter.Fill(this.northwindDataSet.Products);

		}

		private void commandLink1_Click(object sender, EventArgs e)
		{
			splitButton1.Image = imageList1.Images[0];
		}
	}
}
